package com.example.questionexam.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.questionexam.common.BaseResponse;
import com.example.questionexam.common.DeleteRequest;
import com.example.questionexam.common.ErrorCode;
import com.example.questionexam.common.ResultUtils;
import com.example.questionexam.exception.BusinessException;
import com.example.questionexam.exception.ThrowUtils;
import com.example.questionexam.model.dto.teacher.TeacherAddRequest;
import com.example.questionexam.model.dto.teacher.TeacherQueryRequest;
import com.example.questionexam.model.dto.teacher.TeacherUpdateRequest;
import com.example.questionexam.model.entity.Teacher;
import com.example.questionexam.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author Maobohe
 * @createData 2024/5/28 10:35
 */
@RestController
@RequestMapping("/teacher")
@Slf4j
public class TeacherController {

    @Resource
    private TeacherService teacherService;

    /**
     * 创建老师
     *
     * @param teacherAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addTeacher(@RequestBody TeacherAddRequest teacherAddRequest, HttpServletRequest request) {
        if (teacherAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherAddRequest, teacher);
        // 校验
        teacherService.validTeacher(teacher, true);
        boolean result = teacherService.save(teacher);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(teacher.getId());
    }

    /**
     * 删除老师
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteTeacher(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        Long teacher_id = deleteRequest.getId();
        queryWrapper.eq("id", teacher_id);
        boolean remove = teacherService.remove(queryWrapper);
        return ResultUtils.success(remove);
    }

    /**
     * 更新题目
     *
     * @param teacherUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateTeacher(@RequestBody TeacherUpdateRequest teacherUpdateRequest,
                                              HttpServletRequest request) {
        if (teacherUpdateRequest == null || teacherUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherUpdateRequest, teacher);
        // 校验
        teacherService.validTeacher(teacher, false);
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", teacher.getId());
        Teacher oldAnswer = teacherService.getOne(queryWrapper);
        ThrowUtils.throwIf(oldAnswer == null, ErrorCode.NOT_FOUND_ERROR);
        boolean result = teacherService.update(teacher, queryWrapper);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 根据id获取老师
     * @param teacher_id
     * @param request
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<Teacher> getTeacherById(long teacher_id, HttpServletRequest request) {
        if (teacher_id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Teacher teacher = teacherService.getById(teacher_id);
        ThrowUtils.throwIf(teacher == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(teacher);
    }

    /**
     * 分页获取试卷列表
     * @param teacherQueryRequest
     * @param request
     * @return
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<Teacher>> listTeacherByPage(TeacherQueryRequest teacherQueryRequest,
                                                     HttpServletRequest request) {
        long current = teacherQueryRequest.getCurrent();
        long size = teacherQueryRequest.getPageSize();
        Page<Teacher> teacherPage = teacherService.page(new Page<>(current, size),
                teacherService.getQueryWrapper(teacherQueryRequest));
        return ResultUtils.success(teacherPage);
    }


}


